<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no"  />
    <title>
        Query features from a FeatureLayerView | Sample | ArcGIS API for
        JavaScript 4.18
    </title>
    <style>
        html,
        body,
        #sceneDiv {
            height: 100%;
            width: 100%;
            margin: 0;
            padding: 0;
        }

        .panel-container {
            position: relative;
            width: 100%;
            height: 100%;
        }

        .panel-side {
            padding: 2px;
            box-sizing: border-box;
            width: 300px;
            height: 100%;
            position: absolute;
            top: 0;
            right: 0;
            color: #fff;
            background-color: rgba(0, 0, 0, 0.6);
            overflow: auto;
            z-index: 60;
        }

        .panel-side h3 {
            padding: 0 20px;
            margin: 20px 0;
        }

        .panel-side ul {
            list-style: none;
            margin: 0;
            padding: 0;
        }

        .panel-side li {
            list-style: none;
            padding: 10px 20px;
        }

        .panel-result {
            cursor: pointer;
            margin: 2px 0;
            background-color: rgba(0, 0, 0, 0.3);
        }

        .panel-result:hover,
        .panel-result:focus {
            color: orange;
            background-color: rgba(0, 0, 0, 0.75);
        }
    </style>
    <link rel="stylesheet" href="https://js.arcgis.com/4.18/esri/themes/light/main.css" />
    <script src="https://js.arcgis.com/4.18/"></script>

    <script>
        require([
            "esri/Map",
            "esri/views/MapView",
            "esri/layers/FeatureLayer"
        ], function (Map, MapView, FeatureLayer) {

            //创建弹出模板
            const popupTemplate = {
                // autocasts as new PopupTemplate()
                title: "{NAME} in {COUNTY}",
                content: [
                    {
                        type: "fields",
                        fieldInfos: [
                            {
                                fieldName: "B12001_calc_pctMarriedE",
                                label: "% Married",
                                format: {
                                    places: 0,
                                    digitSeparator: true
                                }
                            },
                            {
                                fieldName: "B12001_calc_numMarriedE",
                                label: "Total Married",
                                format: {
                                    places: 0,
                                    digitSeparator: true
                                }
                            },
                            {
                                fieldName: "B12001_calc_numNeverE",
                                label: "Never Married",
                                format: {
                                    places: 0,
                                    digitSeparator: true
                                }
                            },
                            {
                                fieldName: "B12001_calc_numDivorcedE",
                                label: "Total Divorced",
                                format: {
                                    places: 0,
                                    digitSeparator: true
                                }
                            }
                        ]
                    }
                ]
            };

            const featureLayer = new FeatureLayer({
                url:"https://services.arcgis.com/P3ePLMYs2RVChkJx/ArcGIS/rest/services/ACS_Marital_Status_Boundaries/FeatureServer/2",
                outFields: ["NAME", "GEOID"],
                popupTemplate: popupTemplate,
            });

            const map = new Map({
                basemap: "dark-gray-vector",
                layers:[featureLayer]
            });

            const view = new MapView({
                container: "sceneDiv",
                map: map,
                center: [-73.95, 40.702],
                zoom: 12,
                padding: {
                    right: 300
                }
            });

            const listNode = document.getElementById("nyc_graphics");

            let graphics;

            view.whenLayerView(featureLayer).then(function (layerView) {
                layerView.watch("updating", function (value) {
                    if (!value) {
                        layerView.queryFeatures({
                                geometry: view.extent,
                                returnGeometry: true,
                                orderByFields: ["GEOID"]
                            }).then(function (results) {
                                graphics = results.features;

                                const fragment = document.createDocumentFragment();

                                graphics.forEach(function (result, index) {
                                    const attributes = result.attributes;
                                    const name = attributes.NAME;

                                    //在纽约创建一个邮政编码列表
                                    const li = document.createElement("li");
                                    li.classList.add("panel-result");
                                    li.tabIndex = 0;
                                    li.setAttribute("data-result-id", index);
                                    li.textContent = name;

                                    fragment.appendChild(li);
                                });
                                //在纽约创建一个邮政编码列表
                                listNode.innerHTML = "";
                                listNode.appendChild(fragment);
                            })
                            .catch(function (error) {
                                console.error("query failed: ", error);
                            });
                    }
                });
            });

            listNode.addEventListener("click", onListClickHandler);

            function onListClickHandler(event) {
                const target = event.target;
                const resultId = target.getAttribute("data-result-id");

                //获取与点击的邮政编码对应的图形
                const result = resultId && graphics && graphics[parseInt(resultId, 10)];

                if (result) {
                    //在邮政编码多边形的质心处打开弹出窗口并设置将填充弹出内容和标题的弹出窗口的功能
                    view.goTo(result.geometry.extent.expand(2)).then(function () {
                            view.popup.open({
                                features: [result],
                                location: result.geometry.centroid
                            });
                        })
                        .catch(function (error) {
                            if (error.name != "AbortError") {
                                console.error(error);
                            }
                        });
                }
            }
        });
    </script>
</head>

<body>
<div class="panel-container">
    <div class="panel-side esri-widget">
        <h3>Marriage by Census Tract</h3>
        <ul id="nyc_graphics">
            <li>Loading&hellip;</li>
        </ul>
    </div>
    <div id="sceneDiv"></div>
</div>
</body>
</html>
